package cn.szzsi._admin;

import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.MemberRank;
import cn.szzsi.common.model.SiteConfig;
import cn.szzsi.core.kit.StringUtils;
import com.jfinal.aop.Before;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.ehcache.CacheName;
import com.jfinal.plugin.ehcache.EvictInterceptor;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Date;

/**
 *
 *
 */
public class SiteConfigController extends BaseAdminController<SiteConfig> {
    private String shopId;

    public void add(){
        shopId=getAttr("shopId");
        Member member = getLoginMember();
        SiteConfig siteConfig= SiteConfig.getByShopId(shopId);
        setAttr("loginMember", member);
        setAttr("siteConfig",siteConfig);
        setAttr("shopId",shopId);
        render("siteconfig_add.html");
    }

    public void view(){
        shopId=getAttr("shopId");
        SiteConfig siteConfig= SiteConfig.getByShopId(shopId);
        Member member = getLoginMember();
        setAttr("loginMember", member);
        setAttr("siteConfig",siteConfig);
        render("siteconfig_view.html");
    }




    @Before(EvictInterceptor.class)
    @CacheName("SiteConfig")
    public void update(){
        shopId=getAttr("shopId");
        SiteConfig siteConfig = getModel(SiteConfig.class);
        String weixinIndexAddress="/b/weiXinIndex?shopId="+shopId;
        siteConfig.setWeixinIndexAddress(weixinIndexAddress);
        //判断=是否已存在appid和token,若存在,保存原来的,若不在,则重新创建
        String apiid="";
        String token="";
        SiteConfig isExitsiteConfig = SiteConfig.getByShopId(shopId);
        if(isExitsiteConfig!=null){//siteConfig存在
            apiid=siteConfig.getStr("apiid");
            token=siteConfig.getStr("token");
            if(StrKit.isBlank(isExitsiteConfig.getStr("apiid"))){
                if(StrKit.notBlank(siteConfig.getStr("appid"))){
                    apiid = DigestUtils.md5Hex(siteConfig.getStr("appid"));
                }else{
                    apiid = DigestUtils.md5Hex("");
                }
            }

            if(StrKit.isBlank(siteConfig.getStr("token"))){
                token = StringUtils.random(6, StringUtils.RandomType.ALL);
            }
            siteConfig.set("apiid",apiid);
            siteConfig.set("token",token);
        }else{//siteConfig不存在
            if(StrKit.notBlank(siteConfig.getStr("appid"))){
                apiid = DigestUtils.md5Hex(siteConfig.getStr("appid"));
            }else{
                apiid = DigestUtils.md5Hex("");
            }
            token = StringUtils.random(6, StringUtils.RandomType.ALL);
            siteConfig.set("apiid",apiid);
            siteConfig.set("token",token);
        }

        //判断店铺别名是否重复
        if(SiteConfig.getByShopAliasAndUid(siteConfig.getStr("shopAlias"),shopId)!=null){
            renderJson(Ret.fail("info","该店铺别名已存在!"));
            //addActionError("该店铺别名已存在!");
            return;
        }
        //如果没有短信签名,则给个默认值
        if(StrKit.isBlank(siteConfig.getSmsSignature())){
            siteConfig.setSmsSignature("微米荟商城");
        }


        if(isExitsiteConfig!=null){
            //数据库中已存在该会员资料
            siteConfig.update();
        }else{
            //数据库中未存在该会员资料
            siteConfig.set("uid",shopId);
            siteConfig.save();

            //生成默认会员等级数据
            MemberRank memberRank = new MemberRank();
            memberRank.set("createDate", new Date()).set("modifyDate", new Date()).set("isDefault",true).set("name","普通会员").set("point",0).set("preferentialScale",100).set("shopId",shopId);
            memberRank.save(memberRank);
        }
        renderJson(Ret.ok("info","修改成功!").set("url","view"));

    }

    /*@Before(EvictInterceptor.class)
    @CacheName("SiteConfig")
    public void update(){
        SiteConfig siteConfig = getModel(SiteConfig.class);
        System.out.println(siteConfig.getStr("name"));
        String logoUrl=getPara("logoUrl");
        String uid=getPara("uid");
        String shopName=getPara("shopName");
        String shopAlias=getPara("shopAlias");//店铺别名
        String shopUrl=getPara("shopUrl");
        String adminLogo=getPara("adminLogo");
        String hotSearch=getPara("hotSearch");
        String address=getPara("address");
        String phone=getPara("phone");
        String zipCode=getPara("zipCode");
        String email=getPara("email");
        String metaKeywords=getPara("metaKeywords");
        String metaDescription=getPara("metaDescription");
        int openWeixnLogin=0;//默认没开启
        if(StrKit.notBlank(getPara("openWeixnLogin"))){
            openWeixnLogin= Integer.parseInt(getPara("openWeixnLogin"));
        }
        //String apiid=getPara("apiid");
        String appid=getPara("appid");
        String secret=getPara("secret");

        int openFenxiao=getParaToInt("openFenxiao",0);//是否开启分销
        int openZongjian=getParaToInt("openZongjian",0);//是否开启总监奖金
        int distributionLevels=getParaToInt("distributionLevels",0);
        String rewardPercent=getPara("rewardPercent");
        String depositName=getPara("depositName");
        String shopBackName=getPara("shopBackName");
        int firstSignPoint=0;
        if(StrKit.notBlank(getPara("firstSignPoint"))){
            firstSignPoint=getParaToInt("firstSignPoint",0);
        }
        int signPoint=0;
        if(StrKit.notBlank(getPara("signPoint"))){
            signPoint=getParaToInt("signPoint",0);
        }
        int continueSingDays=0;
        if(StrKit.notBlank(getPara("continueSingDays"))){
            continueSingDays=getParaToInt("continueSingDays",0);
        }
        int extraContinuePoint=0;
        if(StrKit.notBlank(getPara("extraContinuePoint"))){
            extraContinuePoint=getParaToInt("extraContinuePoint");
        }
        String weixinIndexAddress="/b/weiXinIndex?shopId="+uid+"";
        SiteConfig siteConfig = SiteConfig.getByShopId(getPara("uid"));

        //判断=是否已存在appid和token,若存在,保存原来的,若不在,则重新创建
        String apiid="";
        String token="";
        if(siteConfig!=null){
            apiid=siteConfig.getStr("apiid");
            token=siteConfig.getStr("token");
            if(StrKit.isBlank(siteConfig.getStr("apiid"))){
                apiid = DigestUtils.md5Hex(appid);
            }
            if(StrKit.isBlank(siteConfig.getStr("token"))){
                token = StringUtils.random(6, StringUtils.RandomType.ALL);
            }
        }else{
            apiid = DigestUtils.md5Hex(appid);
            token = StringUtils.random(6, StringUtils.RandomType.ALL);
        }


        //短信配置
        String sms_account_sid = getPara("sms_account_sid");
        String sms_auth_token = getPara("sms_auth_token");
        String sms_server_ip = getPara("sms_server_ip");
        String sms_server_port = getPara("sms_server_port");
        String sms_appid = getPara("sms_appid");
        String sms_template_id = getPara("sms_template_id");
        //分销配置start
        int faxian1 = getParaToInt("faxian1",0);
        int faxian2 = getParaToInt("faxian2",0);
        int faxian3 = getParaToInt("faxian3",0);

        int rewardnum1 = getParaToInt("rewardnum1",0);
        int rewardnum2 = getParaToInt("rewardnum2",0);
        int rewardnum3 = getParaToInt("rewardnum3",0);
        int rewardnum4 = getParaToInt("rewardnum4",0);
        int rewardnum5 = getParaToInt("rewardnum5",0);
        int rewardnum6 = getParaToInt("rewardnum6",0);

        int rewardPercent1 = getParaToInt("rewardPercent1",0);
        int rewardPercent2 = getParaToInt("rewardPercent2",0);
        int rewardPercent3 = getParaToInt("rewardPercent3",0);
        int rewardPercent4 = getParaToInt("rewardPercent4",0);
        int rewardPercent5 = getParaToInt("rewardPercent5",0);
        int rewardPercent6 = getParaToInt("rewardPercent6",0);
        //分销配置end

        //辅导津贴start
        int fdjtPercent=getParaToInt("fdjtPercent",0);
        int totalIncome1=getParaToInt("totalIncome1",0);
        int totalIncome2=getParaToInt("totalIncome2",0);
        int totalIncome3=getParaToInt("totalIncome3",0);
        int totalIncome4=getParaToInt("totalIncome4",0);
        int totalIncome5=getParaToInt("totalIncome5",0);
        int totalIncome6=getParaToInt("totalIncome6",0);

        int orderPercent1=getParaToInt("orderPercent1",0);
        int orderPercent2=getParaToInt("orderPercent2",0);
        int orderPercent3=getParaToInt("orderPercent3",0);
        int orderPercent4=getParaToInt("orderPercent4",0);
        int orderPercent5=getParaToInt("orderPercent5",0);
        int orderPercent6=getParaToInt("orderPercent6",0);
        //辅导津贴end

        //仓库预警设置,默认为0
        int storeWarning=0;
        if(StrKit.notBlank(getPara("storeWarning"))){
            storeWarning=getParaToInt("storeWarning",0);
        }


        //判断店铺别名是否重复
        if(SiteConfig.getByShopAliasAndUid(shopAlias,getUid())!=null){
            addActionError("该店铺别名已存在!");
            return;
        }
        if(siteConfig!=null){
            //数据库中已存在该会员资料
            SiteConfig.updateSiteConfig(fdjtPercent,totalIncome1,totalIncome2,totalIncome3,totalIncome4,totalIncome5,totalIncome6,orderPercent1,orderPercent2,orderPercent3,orderPercent4,orderPercent5,orderPercent6,faxian1,faxian2,faxian3,rewardnum1,rewardnum2,rewardnum3,rewardnum4,rewardnum5,rewardnum6,rewardPercent1,rewardPercent2,rewardPercent3,rewardPercent4,rewardPercent5,rewardPercent6,sms_account_sid,sms_auth_token,sms_server_ip,sms_server_port,sms_appid,sms_template_id,siteConfig,uid,shopName,shopAlias,logoUrl,shopUrl,hotSearch,address,phone,zipCode,email,metaKeywords,metaDescription,openWeixnLogin,appid,distributionLevels,rewardPercent,secret,weixinIndexAddress,apiid,openFenxiao,openZongjian,depositName,shopBackName,firstSignPoint,signPoint,continueSingDays,extraContinuePoint,storeWarning,token,adminLogo);
        }else{
            //数据库中未存在该会员资料
            SiteConfig.saveSiteConfig(fdjtPercent,totalIncome1,totalIncome2,totalIncome3,totalIncome4,totalIncome5,totalIncome6,orderPercent1,orderPercent2,orderPercent3,orderPercent4,orderPercent5,orderPercent6,faxian1,faxian2,faxian3,rewardnum1,rewardnum2,rewardnum3,rewardnum4,rewardnum5,rewardnum6,rewardPercent1,rewardPercent2,rewardPercent3,rewardPercent4,rewardPercent5,rewardPercent6,sms_account_sid,sms_auth_token,sms_server_ip,sms_server_port,sms_appid,sms_template_id,uid,shopName,shopAlias,logoUrl,shopUrl,hotSearch,address,phone,zipCode,email,metaKeywords,metaDescription,openWeixnLogin,appid,distributionLevels,rewardPercent,secret,weixinIndexAddress,apiid,openFenxiao,openZongjian,depositName,shopBackName,firstSignPoint,signPoint,continueSingDays,extraContinuePoint,storeWarning,token,adminLogo);

            //生成默认会员等级数据
            MemberRank memberRank = new MemberRank();
            memberRank.set("createDate", new Date()).set("modifyDate", new Date()).set("isDefault",true).set("name","普通会员").set("point",0).set("preferentialScale",100).set("shopId",uid);
            memberRank.save(memberRank);
        }
        redirect("/shop/siteconfig/add");

    }*/

}

















